package queue;

import java.util.Stack;

//                                 用两栈来实现队列
class MyQueue {
    //1.准备两个栈 A 和 B
    Stack<Integer> A = new Stack<>();
    Stack<Integer> B = new Stack<>();

    public MyQueue() {

    }
    //入队列就 先把 B 倒腾到 A 中
    //然后往 A 中入栈
    public void push(int x) {
        while (!B.isEmpty()){
            A.push(B.pop());
        }
        A.push(x);
    }
    //出队列 则 先把 A 的倒腾到 B 中
    //再出栈
    public int pop() {
        if (empty()){
            return 0;
        }
        while (!A.isEmpty()){
            B.push(A.pop());
        }
        return B.pop();
    }

    public int peek() {
        if (empty()){
            return 0;
        }
        while (!A.isEmpty()){
            B.push(A.pop());
        }
        return B.peek();
    }

    public boolean empty() {
        return (A.isEmpty() && B.isEmpty());
    }
}
